package es.ats.codington.festival.services;

import java.util.ArrayList;
import java.util.List;

import es.ats.codington.festival.daos.ManagerDAO;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.exceptions.DAOException;
import es.ats.codington.festival.exceptions.ServiceException;
import es.ats.codington.festival.interfaces.daos.IPlaceDAO;
import es.ats.codington.festival.interfaces.services.IPlaceService;

/**
 * 
 * @author Codington_RED
 *
 */
public class PlaceService implements IPlaceService{
	
	
	/**
	 * method that obtains a list with all places registered into the system
	 * @return allPlaces
	 * @throws ServiceException
	 */
	@Override
	 public List<Place> getAllPlaces() throws ServiceException {	
			
			ManagerDAO manager = null;
			IPlaceDAO placeDAO = null;
			List<Place> allPlaces = new ArrayList<Place>();	
				
			try {	    	  
			    manager = new ManagerDAO();
			    placeDAO = manager.getPlaceDAO();
			    
			    allPlaces = placeDAO.showAllPlaces();
			    
			    manager.closeConnection();
			    
			} catch(DAOException ex) {	
			    if(manager != null) {
			    	
			    	manager.closeError();
				
			    }
		    
			    throw new ServiceException("Error accessing database", ex);
		    
			}
			
			return allPlaces;
	 }
	
	/**
	 * method that modifies all the information about a determined place 
	 * @param place
	 * @return affectedRows
	 * @throws ServiceException
	 */
	@Override
    public int updatePlace(Place place) throws ServiceException {
	
		ManagerDAO manager = null;
		IPlaceDAO placeDAO = null;
		Integer affectedRows = 0;
			
		try {
		    manager = new ManagerDAO();
		    placeDAO = manager.getPlaceDAO();
		    
		    affectedRows = placeDAO.updatePlace(place);
		    
		} catch(DAOException ex) {	    
		    throw new ServiceException("Error accessing database", ex);
		    
		} finally {
		    if(manager != null) {		
			if(affectedRows == 1) {
			    manager.closeConnection();
			    
			} else {
			    manager.closeError();
			    
			}		
		    }	    
		}
				
		return affectedRows;
    }
	
	/**
	 * method that inserts a place into the database
	 * @param place
	 * @return affectedRows
	 * @throws ServiceException
	 */
	@Override
    public boolean insertPlace(Place place, String typePlace) throws ServiceException{
	
    	ManagerDAO manager = null;	
		IPlaceDAO placeDAO = null;
		//ISequencerDAO sequencerDAO = null;
		boolean affectedRows = false;
		//Integer eventId = 0;
		
		try {
		    manager = new ManagerDAO();
		    placeDAO = manager.getPlaceDAO();
		  
		    //sequencerDAO = manager.getSequencerDAO();
		    	    
		    //eventId = sequencerDAO.getNext(ISequencerDAO.CLASS_EVENT_NAME);
		    //event.setEventId(eventId);
		    
		    affectedRows = placeDAO.insertPlace(place, typePlace);
		   
		    
		} catch(DAOException ex) {	    
		    throw new ServiceException("Error accessing database", ex);
		    
		} finally {
		    if(manager != null) {
		    	if(affectedRows == true) {
		    		manager.closeConnection();
			    
		    	} else {
		    		manager.closeError();
			    }		
		    }
		}
			
		return affectedRows;
    }

}

